﻿using CoreWCF;
using SD.Infrastructure.DTOBase;
using SlamDunk.MES.AppService.Maps;
using SlamDunk.MES.Domain.Entities.AcquisitionContext;
using SlamDunk.MES.Domain.Entities.ExecutionContext;
using SlamDunk.MES.Domain.Entities.FoundationContext;
using SlamDunk.MES.Domain.Entities.PlanContxt;
using SlamDunk.MES.Domain.IRepositories;
using SlamDunk.MES.Domain.Mediators;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.FoundationContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.PlanContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.DTOs.Outputs.StockContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;
using System.Linq;

namespace SlamDunk.MES.AppService.Implements
{
    /// <summary>
    /// 作业管理服务契约实现
    /// </summary>
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
    public class ExecutionContract : IExecutionContract
    {
        #region # 字段及构造器

        /// <summary>
        /// 领域服务中介者
        /// </summary>
        private readonly DomainServiceMediator _svcMediator;

        /// <summary>
        /// 仓储中介者
        /// </summary>
        private readonly RepositoryMediator _repMediator;

        /// <summary>
        /// 单元事务
        /// </summary>
        private readonly IUnitOfWorkMES _unitOfWork;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ExecutionContract(DomainServiceMediator svcMediator, RepositoryMediator repMediator, IUnitOfWorkMES unitOfWork)
        {
            this._svcMediator = svcMediator;
            this._repMediator = repMediator;
            this._unitOfWork = unitOfWork;
        }

        #endregion


        //命令部分

        #region # 生成工单 —— void GenerateWorkOrders(Guid executionPlanId)
        /// <summary>
        /// 生成工单
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        public void GenerateWorkOrders(Guid executionPlanId)
        {
            ExecutionPlan executionPlan = this._unitOfWork.Resolve<ExecutionPlan>(executionPlanId);
            ICollection<WorkOrder> workOrders = this._svcMediator.WorkOrderSvc.GenerateWorkOrders(executionPlan);

            executionPlan.GenerateWorkOrders();

            this._unitOfWork.RegisterSave(executionPlan);
            this._unitOfWork.RegisterAddRange(workOrders);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 修改工单 —— void UpdateWorkOrder(Guid workOrderId, string workOrderName...
        /// <summary>
        /// 修改工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="workOrderName">工单名称</param>
        /// <param name="durationQuota">工时定额</param>
        /// <param name="directivePath">工艺指导书</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="workStationId">工位Id</param>
        /// <param name="needToInspect">是否需质检</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        public void UpdateWorkOrder(Guid workOrderId, string workOrderName, TimeSpan durationQuota, string directivePath, Guid? facilityId, Guid? workStationId, bool needToInspect, int sort, string description)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.UpdateInfo(workOrderName, durationQuota, directivePath, facilityId, workStationId, needToInspect, sort, description);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单人员要求 —— void SaveWorkOrderPersonAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单人员要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="personAskParams">工单人员要求参数模型集</param>
        public void SaveWorkOrderPersonAsks(Guid workOrderId, IEnumerable<WorkOrderPersonAskParam> personAskParams)
        {
            personAskParams = personAskParams?.ToArray() ?? Array.Empty<WorkOrderPersonAskParam>();
            IEnumerable<WorkOrderPersonAsk> personAsks =
                from personAskParam in personAskParams
                select new WorkOrderPersonAsk(personAskParam.personClassId, personAskParam.personId, personAskParam.personUse, personAskParam.quantity, personAskParam.facilityId, personAskParam.sort, personAskParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetPersonAsks(personAsks);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单设备要求 —— void SaveWorkOrderEquipmentAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单设备要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="equipmentAskParams">工单设备要求参数模型集</param>
        public void SaveWorkOrderEquipmentAsks(Guid workOrderId, IEnumerable<WorkOrderEquipmentAskParam> equipmentAskParams)
        {
            equipmentAskParams = equipmentAskParams?.ToArray() ?? Array.Empty<WorkOrderEquipmentAskParam>();
            IEnumerable<WorkOrderEquipmentAsk> equipmentAsks =
                from equipmentAskParam in equipmentAskParams
                select new WorkOrderEquipmentAsk(equipmentAskParam.equipmentClassId, equipmentAskParam.equipmentId, equipmentAskParam.equipmentUse, equipmentAskParam.quantity, equipmentAskParam.facilityId, equipmentAskParam.sort, equipmentAskParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetEquipmentAsks(equipmentAsks);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单物料要求 —— void SaveWorkOrderMaterialAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单物料要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="materialAskParams">工单物料要求参数模型集</param>
        public void SaveWorkOrderMaterialAsks(Guid workOrderId, IEnumerable<WorkOrderMaterialAskParam> materialAskParams)
        {
            materialAskParams = materialAskParams?.ToArray() ?? Array.Empty<WorkOrderMaterialAskParam>();
            IEnumerable<WorkOrderMaterialAsk> materialAsks =
                from materialAskParam in materialAskParams
                select new WorkOrderMaterialAsk(materialAskParam.materialId, materialAskParam.materialLotId, materialAskParam.directionType, materialAskParam.quantity, materialAskParam.facilityId, materialAskParam.sort, materialAskParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetMaterialAsks(materialAsks);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单参数要求 —— void SaveWorkOrderParameterAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单参数要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="parameterAskParams">工单参数要求参数模型集</param>
        public void SaveWorkOrderParameterAsks(Guid workOrderId, IEnumerable<WorkOrderParameterAskParam> parameterAskParams)
        {
            parameterAskParams = parameterAskParams?.ToArray() ?? Array.Empty<WorkOrderParameterAskParam>();
            IEnumerable<WorkOrderParameterAsk> parameterAsks =
                from parameterAskParam in parameterAskParams
                select new WorkOrderParameterAsk(parameterAskParam.parameterNo, parameterAskParam.parameterName, parameterAskParam.relatedVariableNo, parameterAskParam.standardValue, parameterAskParam.maxValue, parameterAskParam.minValue, parameterAskParam.valueUnitNo, parameterAskParam.facilityId, parameterAskParam.sort, parameterAskParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetParameterAsks(parameterAsks);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 准备工单 —— void ReadyWorkOrder(Guid workOrderId)
        /// <summary>
        /// 准备工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        public void ReadyWorkOrder(Guid workOrderId)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.Ready();

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 取消工单 —— void CancelWorkOrder(string reason)
        /// <summary>
        /// 取消工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="reason">取消原因</param>
        public void CancelWorkOrder(Guid workOrderId, string reason)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.Cancel(reason);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 分派工单到班组 —— void DispatchWorkOrderToTeam(Guid workOrderId, Guid teamId)
        /// <summary>
        /// 分派工单到班组
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="teamId">班组Id</param>
        public void DispatchWorkOrderToTeam(Guid workOrderId, Guid teamId)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.DispatchTeam(teamId);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 分派工单到人员 —— void DispatchWorkOrderToPerson(Guid workOrderId, Guid personId)
        /// <summary>
        /// 分派工单到人员
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="personId">人员Id</param>
        public void DispatchWorkOrderToPerson(Guid workOrderId, Guid personId)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.DispatchPerson(personId);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 执行工单 —— void ExecuteWorkOrder(Guid workOrderId)
        /// <summary>
        /// 执行工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        public void ExecuteWorkOrder(Guid workOrderId)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.Execute();

            #region //执行工单相关作业计划、生产计划

            ExecutionPlan executionPlan = this._unitOfWork.Resolve<ExecutionPlan>(workOrder.ExecutionPlanId);
            executionPlan.Execute();
            if (executionPlan.ProductionPlanId.HasValue)
            {
                ProductionPlan productionPlan = this._unitOfWork.Resolve<ProductionPlan>(executionPlan.ProductionPlanId.Value);
                productionPlan.Execute();

                this._unitOfWork.RegisterSave(productionPlan);
            }
            this._unitOfWork.RegisterSave(executionPlan);

            #endregion

            #region //创建采集会话

            AcquisitionRule acquisitionRule;
            AcquisitionSession acquisitionSession = null;
            if (this._repMediator.AcquisitionSessionRep.Exists(workOrder.Id))
            {
                acquisitionSession = this._unitOfWork.Resolve<AcquisitionSession>(workOrder.Id);
                acquisitionRule = this._repMediator.AcquisitionRuleRep.SingleByProcessSegment(acquisitionSession.AcquisitionRuleId);
            }
            else
            {
                acquisitionRule = this._repMediator.AcquisitionRuleRep.SingleByProcessSegment(workOrder.ProcessSegmentId);
                if (acquisitionRule != null)
                {
                    acquisitionSession = new AcquisitionSession(acquisitionRule.Id, acquisitionRule.Interval, executionPlan.ProcessId, workOrder.ProcessSegmentId, executionPlan.Id, workOrder.Id);
                    this._unitOfWork.RegisterAdd(acquisitionSession);
                }
            }

            #endregion

            #region //TODO 调度采集定时任务

            if (acquisitionSession != null)
            {
                //IEnumerable<string> variableNos = acquisitionRule.Items.Select(x => x.VariableNo);
                //AcquisitionCrontab crontab = new AcquisitionCrontab(acquisitionSession.Id, acquisitionSession.AcquisitionRuleId, acquisitionSession.Interval, variableNos);
                //this._crontabContract.ScheduleCrontab(crontab);
            }

            #endregion

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 中止工单 —— void AbortWorkOrder(Guid workOrderId, string reason)
        /// <summary>
        /// 中止工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="reason">中止原因</param>
        public void AbortWorkOrder(Guid workOrderId, string reason)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.Abort(reason);

            //TODO 暂停采集定时任务
            //this._crontabContract.PauseCrontab(workOrder.Id.ToString());

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 完成工单 —— void CompleteWorkOrder(Guid workOrderId)
        /// <summary>
        /// 完成工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        public void CompleteWorkOrder(Guid workOrderId)
        {
            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.Complete();

            //TODO 删除采集定时任务
            //this._crontabContract.RemoveCrontab(workOrder.Id.ToString());

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单实际人员 —— void SaveWorkOrderActualPersons(Guid workOrderId...
        /// <summary>
        /// 保存工单实际人员
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualPersonParams">工单实际人员参数模型集</param>
        public void SaveWorkOrderActualPersons(Guid workOrderId, IEnumerable<WorkOrderActualPersonParam> actualPersonParams)
        {
            actualPersonParams = actualPersonParams?.ToArray() ?? Array.Empty<WorkOrderActualPersonParam>();
            IEnumerable<WorkOrderActualPerson> actualPersons =
                from actualPersonParam in actualPersonParams
                select new WorkOrderActualPerson(actualPersonParam.personId, actualPersonParam.personUse, actualPersonParam.actualStartTime, actualPersonParam.actualEndTime, actualPersonParam.sort, actualPersonParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetActualPersons(actualPersons);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单实际设备 —— void SaveWorkOrderActualEquipments(Guid workOrderId...
        /// <summary>
        /// 保存工单实际设备
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualEquipmentParams">工单实际设备参数模型集</param>
        public void SaveWorkOrderActualEquipments(Guid workOrderId, IEnumerable<WorkOrderActualEquipmentParam> actualEquipmentParams)
        {
            actualEquipmentParams = actualEquipmentParams?.ToArray() ?? Array.Empty<WorkOrderActualEquipmentParam>();
            IEnumerable<WorkOrderActualEquipment> actualEquipments =
                from actualEquipmentParam in actualEquipmentParams
                select new WorkOrderActualEquipment(actualEquipmentParam.equipmentId, actualEquipmentParam.equipmentUse, actualEquipmentParam.actualStartTime, actualEquipmentParam.actualEndTime, actualEquipmentParam.sort, actualEquipmentParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetActualEquipments(actualEquipments);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单实际物料投入 —— void SaveWorkOrderActualInputMaterials(Guid workOrderId...
        /// <summary>
        /// 保存工单实际物料投入
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualMaterialInputParams">工单实际物料投入参数模型集</param>
        public void SaveWorkOrderActualInputMaterials(Guid workOrderId, IEnumerable<WorkOrderActualMaterialParam> actualMaterialInputParams)
        {
            actualMaterialInputParams = actualMaterialInputParams?.ToArray() ?? Array.Empty<WorkOrderActualMaterialParam>();
            IEnumerable<WorkOrderActualMaterial> actualMaterials =
                from actualInputMaterialParam in actualMaterialInputParams
                select new WorkOrderActualMaterial(actualInputMaterialParam.materialId, actualInputMaterialParam.materialLotId, MaterialDirectionType.Input, actualInputMaterialParam.quantity, actualInputMaterialParam.sort, actualInputMaterialParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetActualInputMaterials(actualMaterials);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单实际物料产出 —— void SaveWorkOrderActualOutputMaterials(Guid workOrderId...
        /// <summary>
        /// 保存工单实际物料产出
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualOutputMaterialParams">工单实际物料产出参数模型集</param>
        public void SaveWorkOrderActualOutputMaterials(Guid workOrderId, IEnumerable<WorkOrderActualMaterialParam> actualOutputMaterialParams)
        {
            actualOutputMaterialParams = actualOutputMaterialParams?.ToArray() ?? Array.Empty<WorkOrderActualMaterialParam>();
            IEnumerable<WorkOrderActualMaterial> actualMaterials =
                from actualOutputMaterialParam in actualOutputMaterialParams
                select new WorkOrderActualMaterial(actualOutputMaterialParam.materialId, actualOutputMaterialParam.materialLotId, MaterialDirectionType.Output, actualOutputMaterialParam.quantity, actualOutputMaterialParam.sort, actualOutputMaterialParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetActualOutputMaterials(actualMaterials);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion

        #region # 保存工单实际参数 —— void SaveWorkOrderActualParameters(Guid workOrderId...
        /// <summary>
        /// 保存工单实际参数
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualParameterParams">工单实际参数参数模型集</param>
        public void SaveWorkOrderActualParameters(Guid workOrderId, IEnumerable<WorkOrderActualParameterParam> actualParameterParams)
        {
            actualParameterParams = actualParameterParams?.ToArray() ?? Array.Empty<WorkOrderActualParameterParam>();
            IEnumerable<WorkOrderActualParameter> actualParameters =
                from actualParameterParam in actualParameterParams
                select new WorkOrderActualParameter(actualParameterParam.parameterNo, actualParameterParam.parameterName, actualParameterParam.relatedVariableNo, actualParameterParam.value, actualParameterParam.valueUnitNo, actualParameterParam.sort, actualParameterParam.description);

            WorkOrder workOrder = this._unitOfWork.Resolve<WorkOrder>(workOrderId);
            workOrder.SetActualParameters(actualParameters);

            this._unitOfWork.RegisterSave(workOrder);
            this._unitOfWork.Commit();
        }
        #endregion


        //查询部分

        #region # 分页获取工单列表 —— PageModel<WorkOrderInfo> GetWorkOrdersByPage(string keywords...
        /// <summary>
        /// 分页获取工单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="productionPlanId">生产计划Id</param>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="workStationId">工位Id</param>
        /// <param name="needToInspect">是否需质检</param>
        /// <param name="hasInspected">是否已质检</param>
        /// <param name="dispatchedTeamId">分派班组Id</param>
        /// <param name="dispatchedPersonId">分派人员Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="statuses">工单状态集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>工单列表</returns>
        public PageModel<WorkOrderInfo> GetWorkOrdersByPage(string keywords, Guid? productionPlanId, Guid? executionPlanId, Guid? processSegmentId, Guid? workStationId, bool? needToInspect, bool? hasInspected, Guid? dispatchedTeamId, Guid? dispatchedPersonId, DateTime? startTime, DateTime? endTime, Guid? facilityId, IList<WorkOrderStatus> statuses, int pageIndex, int pageSize)
        {
            Facility facility = facilityId.HasValue
                ? this._repMediator.FacilityRep.SingleFully(facilityId.Value)
                : null;
            IList<Guid> relatedFacilityIds = facility?.GetDeepNodeIds();

            ICollection<WorkOrder> workOrders = this._repMediator.WorkOrderRep.FindByPage(keywords, productionPlanId, executionPlanId, processSegmentId, workStationId, needToInspect, hasInspected, dispatchedTeamId, dispatchedPersonId, startTime, endTime, statuses, relatedFacilityIds, pageIndex, pageSize, out int rowCount, out int pageCount);

            IEnumerable<Guid> executionPlanIds = workOrders.Select(x => x.ExecutionPlanId);
            IEnumerable<Guid> facilityIds = workOrders.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IEnumerable<Guid> workStationIds = workOrders.Where(x => x.WorkStationId.HasValue).Select(x => x.WorkStationId.Value);
            IEnumerable<Guid> teamIds = workOrders.Where(x => x.DispatchedTeamId.HasValue).Select(x => x.DispatchedTeamId.Value);
            IEnumerable<Guid> personIds = workOrders.Where(x => x.DispatchedPersonId.HasValue).Select(x => x.DispatchedPersonId.Value);
            IDictionary<Guid, ExecutionPlanInfo> executionPlanInfos = this._repMediator.ExecutionPlanRep.Find(executionPlanIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null, null, null, null, null));
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());
            IDictionary<Guid, WorkStationInfo> workStationInfos = this._repMediator.WorkStationRep.Find(workStationIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, TeamInfo> teamInfos = this._repMediator.TeamRep.Find(teamIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, PersonInfo> personInfos = this._repMediator.PersonRep.Find(personIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            IEnumerable<WorkOrderInfo> workOrderInfos = workOrders.Select(x => x.ToDTO(executionPlanInfos, facilityInfos, workStationInfos, teamInfos, personInfos));

            return new PageModel<WorkOrderInfo>(workOrderInfos, pageIndex, pageSize, pageCount, rowCount);
        }
        #endregion

        #region # 获取工单人员要求列表 —— IEnumerable<WorkOrderPersonAskInfo> GetWorkOrderPersonAsks(...
        /// <summary>
        /// 获取工单人员要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单人员要求列表</returns>
        public IEnumerable<WorkOrderPersonAskInfo> GetWorkOrderPersonAsks(Guid workOrderId)
        {
            ICollection<WorkOrderPersonAsk> personAsks = this._repMediator.WorkOrderPersonAskRep.Find(workOrderId);

            IEnumerable<Guid> personClassIds = personAsks.Where(x => x.PersonClassId.HasValue).Select(x => x.PersonClassId.Value);
            IEnumerable<Guid> personIds = personAsks.Where(x => x.PersonId.HasValue).Select(x => x.PersonId.Value);
            IEnumerable<Guid> facilityIds = personAsks.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, PersonClassInfo> personClassInfos = this._repMediator.PersonClassRep.Find(personClassIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, PersonInfo> personInfos = this._repMediator.PersonRep.Find(personIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<WorkOrderPersonAskInfo> personAskInfos = personAsks.Select(x => x.ToDTO(personClassInfos, personInfos, facilityInfos));

            return personAskInfos;
        }
        #endregion

        #region # 获取工单设备要求列表 —— IEnumerable<WorkOrderEquipmentAskInfo> GetWorkOrderEquipmentAsks(...
        /// <summary>
        /// 获取工单设备要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单设备要求列表</returns>
        public IEnumerable<WorkOrderEquipmentAskInfo> GetWorkOrderEquipmentAsks(Guid workOrderId)
        {
            ICollection<WorkOrderEquipmentAsk> equipmentAsks = this._repMediator.WorkOrderEquipmentAskRep.Find(workOrderId);

            IEnumerable<Guid> equipmentClassIds = equipmentAsks.Where(x => x.EquipmentClassId.HasValue).Select(x => x.EquipmentClassId.Value);
            IEnumerable<Guid> equipmentIds = equipmentAsks.Where(x => x.EquipmentId.HasValue).Select(x => x.EquipmentId.Value);
            IEnumerable<Guid> facilityIds = equipmentAsks.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, EquipmentClassInfo> equipmentClassInfos = this._repMediator.EquipmentClassRep.Find(equipmentClassIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, EquipmentInfo> equipmentInfos = this._repMediator.EquipmentRep.Find(equipmentIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<WorkOrderEquipmentAskInfo> equipmentAskInfos = equipmentAsks.Select(x => x.ToDTO(equipmentClassInfos, equipmentInfos, facilityInfos));

            return equipmentAskInfos;
        }
        #endregion

        #region # 获取工单物料要求列表 —— IEnumerable<WorkOrderMaterialAskInfo> GetWorkOrderMaterialAsks(...
        /// <summary>
        /// 获取工单物料要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单物料要求列表</returns>
        public IEnumerable<WorkOrderMaterialAskInfo> GetWorkOrderMaterialAsks(Guid workOrderId)
        {
            ICollection<WorkOrderMaterialAsk> materialAsks = this._repMediator.WorkOrderMaterialAskRep.Find(workOrderId);

            IEnumerable<Guid> materialIds = materialAsks.Select(x => x.MaterialId);
            IEnumerable<Guid> materialLotIds = materialAsks.Where(x => x.MaterialLotId.HasValue).Select(x => x.MaterialLotId.Value);
            IEnumerable<Guid> facilityIds = materialAsks.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, MaterialInfo> materialInfos = this._repMediator.MaterialRep.Find(materialIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, MaterialLotInfo> materialLotInfos = this._repMediator.MaterialLotRep.Find(materialLotIds)
                .ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<WorkOrderMaterialAskInfo> materialAskInfos = materialAsks.Select(x => x.ToDTO(materialInfos, materialLotInfos, facilityInfos));

            return materialAskInfos;
        }
        #endregion

        #region # 获取工单参数要求列表 —— IEnumerable<WorkOrderParameterAskInfo> GetWorkOrderParameterAsks(...
        /// <summary>
        /// 获取工单参数要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单参数要求列表</returns>
        public IEnumerable<WorkOrderParameterAskInfo> GetWorkOrderParameterAsks(Guid workOrderId)
        {
            ICollection<WorkOrderParameterAsk> parameterAsks = this._repMediator.WorkOrderParameterAskRep.Find(workOrderId);

            IEnumerable<Guid> facilityIds = parameterAsks.Where(x => x.FacilityId.HasValue).Select(x => x.FacilityId.Value);
            IDictionary<Guid, FacilityInfo> facilityInfos = this._repMediator.FacilityRep.Find(facilityIds).ToDictionary(x => x.Key, x => x.Value.ToDTO());

            IEnumerable<WorkOrderParameterAskInfo> parameterAskInfos = parameterAsks.Select(x => x.ToDTO(facilityInfos));

            return parameterAskInfos;
        }
        #endregion

        #region # 获取工单实际人员列表 —— IEnumerable<WorkOrderActualPersonInfo> GetWorkOrderActualPersons(...
        /// <summary>
        /// 获取工单实际人员列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际人员列表</returns>
        public IEnumerable<WorkOrderActualPersonInfo> GetWorkOrderActualPersons(Guid workOrderId)
        {
            ICollection<WorkOrderActualPerson> actualPersons = this._repMediator.WorkOrderActualPersonRep.Find(workOrderId);

            IEnumerable<Guid> personIds = actualPersons.Select(x => x.PersonId);
            IDictionary<Guid, PersonInfo> personInfos = this._repMediator.PersonRep.Find(personIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            IEnumerable<WorkOrderActualPersonInfo> actualPersonInfos = actualPersons.Select(x => x.ToDTO(personInfos));

            return actualPersonInfos;
        }
        #endregion

        #region # 获取工单实际设备列表 —— IEnumerable<WorkOrderActualEquipmentInfo> GetWorkOrderActualEquipments(...
        /// <summary>
        /// 获取工单实际设备列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际设备列表</returns>
        public IEnumerable<WorkOrderActualEquipmentInfo> GetWorkOrderActualEquipments(Guid workOrderId)
        {
            ICollection<WorkOrderActualEquipment> actualEquipments = this._repMediator.WorkOrderActualEquipmentRep.Find(workOrderId);

            IEnumerable<Guid> equipmentIds = actualEquipments.Select(x => x.EquipmentId);
            IDictionary<Guid, EquipmentInfo> equipmentInfos = this._repMediator.EquipmentRep.Find(equipmentIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            IEnumerable<WorkOrderActualEquipmentInfo> actualEquipmentInfos = actualEquipments.Select(x => x.ToDTO(equipmentInfos));

            return actualEquipmentInfos;
        }
        #endregion

        #region # 获取工单实际物料列表 —— IEnumerable<WorkOrderActualMaterialInfo> GetWorkOrderActualMaterials(...
        /// <summary>
        /// 获取工单实际物料列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际物料列表</returns>
        public IEnumerable<WorkOrderActualMaterialInfo> GetWorkOrderActualMaterials(Guid workOrderId)
        {
            ICollection<WorkOrderActualMaterial> actualMaterials = this._repMediator.WorkOrderActualMaterialRep.Find(workOrderId);

            IEnumerable<Guid> materialIds = actualMaterials.Select(x => x.MaterialId);
            IEnumerable<Guid> materialLotIds = actualMaterials.Where(x => x.MaterialLotId.HasValue).Select(x => x.MaterialLotId.Value);
            IDictionary<Guid, MaterialInfo> materialInfos = this._repMediator.MaterialRep.Find(materialIds).ToDictionary(x => x.Key, x => x.Value.ToDTO(null));
            IDictionary<Guid, MaterialLotInfo> materialLotInfos = this._repMediator.MaterialLotRep.Find(materialLotIds)
                .ToDictionary(x => x.Key, x => x.Value.ToDTO(null));

            IEnumerable<WorkOrderActualMaterialInfo> actualMaterialInfos = actualMaterials.Select(x => x.ToDTO(materialInfos, materialLotInfos));

            return actualMaterialInfos;
        }
        #endregion

        #region # 获取工单实际参数列表 —— IEnumerable<WorkOrderActualParameterInfo> GetWorkOrderActualParameters(...
        /// <summary>
        /// 获取工单实际参数列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际参数列表</returns>
        public IEnumerable<WorkOrderActualParameterInfo> GetWorkOrderActualParameters(Guid workOrderId)
        {
            ICollection<WorkOrderActualParameter> actualParameters = this._repMediator.WorkOrderActualParameterRep.Find(workOrderId);
            IEnumerable<WorkOrderActualParameterInfo> actualParameterInfos = actualParameters.Select(x => x.ToDTO());

            return actualParameterInfos;
        }
        #endregion
    }
}
